﻿using HKB.Common;
using HKB.Web.Admin;
using System;
using System.Data;
using System.Web.Profile;
using System.Web.SessionState;
using System.Web.UI.WebControls;
using System.Xml;

public partial class Admin_Admin_HelpManage : Admin, IRequiresSessionState
{
    private string sysstr = "";
    protected DefaultProfile profile
    {
        get
        {
            return (DefaultProfile)this.Context.Profile;
        }
    }
    protected global_asax applicationInstance
    {
        get
        {
            return (global_asax)this.Context.ApplicationInstance;
        }
    }
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!base.admin.CheckSuperAdmin())
        {
            base.WriteRightErrMsg();
        }
        this.sysstr = Fetch.Get("sys");
        if (!base.IsPostBack)
        {
            if (this.sysstr != string.Empty || Fetch.Get("action") == "add")
            {
                this.GridView1.Visible = false;
                this.FormView1.Visible = true;
                if (Fetch.Get("action") == "add")
                {
                    this.FormView1.DefaultMode = FormViewMode.Insert;
                }
                else
                {
                    this.BindFormView1();
                }
            }
            else
            {
                this.BindList();
            }
        }
    }
    protected void BindList()
    {
        XmlDocument xmlDocument = new XmlDocument();
        xmlDocument.Load(base.Server.MapPath("~/") + "App_Data\\Help.config");
        DataTable dataTable = new DataTable();
        dataTable.Columns.Add("sys");
        dataTable.Columns.Add("title");
        dataTable.Columns.Add("info");
        foreach (XmlNode xmlNode in xmlDocument.DocumentElement)
        {
            object[] values = new object[]
			{
				xmlNode.Name,
				xmlNode.Attributes["title"].InnerText,
				xmlNode.InnerText
			};
            dataTable.Rows.Add(values);
        }
        this.GridView1.DataSource = dataTable;
        this.GridView1.DataBind();
    }
    protected void BindFormView1()
    {
        XmlDocument xmlDocument = new XmlDocument();
        xmlDocument.Load(base.Server.MapPath("~/") + "App_Data\\Help.config");
        DataTable dataTable = new DataTable();
        dataTable.Columns.Add("sys");
        dataTable.Columns.Add("title");
        dataTable.Columns.Add("info");
        XmlNode xmlNode = xmlDocument.SelectSingleNode("root/" + this.sysstr);
        if (xmlNode != null)
        {
            dataTable.Rows.Add(new object[]
			{
				xmlNode.Name,
				xmlNode.Attributes["title"].InnerText,
				xmlNode.InnerText
			});
        }
        this.FormView1.DataSource = dataTable;
        this.FormView1.DataBind();
    }
    protected void OnSubmit(object sender, EventArgs e)
    {
        TextBox textBox = (TextBox)this.FormView1.FindControl("O_TxtSys");
        TextBox textBox2 = (TextBox)this.FormView1.FindControl("TxtSys");
        TextBox textBox3 = (TextBox)this.FormView1.FindControl("TxtTitle");
        TextBox textBox4 = (TextBox)this.FormView1.FindControl("TxtInfo");
        XmlDocument xmlDocument = new XmlDocument();
        xmlDocument.Load(base.Server.MapPath("~/") + "App_Data\\Help.config");
        XmlNode xmlNode = xmlDocument.SelectSingleNode("root/" + textBox.Text);
        if (xmlNode.Name != textBox.Text)
        {
            XmlNode xmlNode2 = xmlDocument.CreateElement(textBox2.Text);
            xmlNode2.InnerText = textBox4.Text;
            XmlAttribute xmlAttribute = xmlNode2.OwnerDocument.CreateAttribute("title");
            xmlAttribute.InnerText = textBox3.Text;
            xmlNode2.Attributes.Append(xmlAttribute);
            xmlDocument.ReplaceChild(xmlNode, xmlNode2);
        }
        else
        {
            xmlNode.Attributes["title"].InnerText = textBox3.Text;
            xmlNode.InnerText = textBox4.Text;
        }
        try
        {
            xmlDocument.Save(base.Server.MapPath("~/") + "App_Data\\Help.config");
        }
        catch (Exception ex)
        {
            this.WriteErrMsg("", Language.Get("Common/Fail", ex.Message), "");
        }
        this.WriteSuccessMsg("", Language.Get("Common/Success"), "Admin_HelpManage.aspx");
    }
    protected void OnSubmitOnInsert(object sender, EventArgs e)
    {
        TextBox textBox = (TextBox)this.FormView1.FindControl("TxtSys");
        TextBox textBox2 = (TextBox)this.FormView1.FindControl("TxtTitle");
        TextBox textBox3 = (TextBox)this.FormView1.FindControl("TxtInfo");
        if (string.IsNullOrEmpty(textBox.Text.Trim()))
        {
            this.Page.ClientScript.RegisterStartupScript(this.Page.GetType(), "msg", "<script>alert('系统标记不能为空！');</script>");
        }
        else
        {
            if (string.IsNullOrEmpty(textBox2.Text.Trim()))
            {
                this.Page.ClientScript.RegisterStartupScript(this.Page.GetType(), "msg", "<script>alert('标题不能为空！');</script>");
            }
            else
            {
                if (string.IsNullOrEmpty(textBox3.Text.Trim()))
                {
                    this.Page.ClientScript.RegisterStartupScript(this.Page.GetType(), "msg", "<script>alert('内容不能为空！');</script>");
                }
                else
                {
                    XmlDocument xmlDocument = new XmlDocument();
                    xmlDocument.Load(base.Server.MapPath("~/") + "App_Data\\Help.config");
                    XmlNode xmlNode = xmlDocument.SelectSingleNode("root/" + textBox.Text);
                    if (xmlNode != null)
                    {
                        this.WriteErrMsg("", "系统标记重复！", "");
                    }
                    xmlNode = xmlDocument.CreateElement(textBox.Text);
                    XmlAttribute xmlAttribute = xmlNode.OwnerDocument.CreateAttribute("title");
                    xmlAttribute.InnerText = textBox2.Text;
                    xmlNode.Attributes.Append(xmlAttribute);
                    xmlNode.InnerText = textBox3.Text;
                    xmlDocument.DocumentElement.AppendChild(xmlNode);
                    try
                    {
                        xmlDocument.Save(base.Server.MapPath("~/") + "App_Data\\Help.config");
                    }
                    catch (Exception ex)
                    {
                        this.WriteErrMsg("", Language.Get("Common/Fail", ex.Message), "");
                    }
                    this.WriteSuccessMsg("", Language.Get("Common/Success"), "Admin_HelpManage.aspx");
                }
            }
        }
    }
    protected void DelHelp(object sender, CommandEventArgs e)
    {
        if (e.CommandName != null)
        {
            string text = e.CommandName.ToString();
            if (text != "")
            {
                XmlDocument xmlDocument = new XmlDocument();
                xmlDocument.Load(base.Server.MapPath("~/") + "App_Data\\Help.config");
                XmlNode oldChild = xmlDocument.SelectSingleNode("root/" + text);
                xmlDocument.DocumentElement.RemoveChild(oldChild);
                try
                {
                    xmlDocument.Save(base.Server.MapPath("~/") + "App_Data\\Help.config");
                }
                catch (Exception ex)
                {
                    this.WriteErrMsg("", Language.Get("Common/Fail", ex.Message), "");
                }
                this.WriteSuccessMsg("", Language.Get("Common/Success"), "Admin_HelpManage.aspx");
            }
        }
    }
}
